package com.binguo.wxapi.controller;

import com.binguo.helper.PhoneHelper;
import com.binguo.model.model.CustomerShop;
import com.binguo.wxapi.form.CustomerForm;
import com.binguo.wxapi.form.CustomerGroupForm;
import com.binguo.wxapi.form.MerchantLoginForm;
import com.binguo.wxapi.result.Result;
import com.binguo.wxapi.result.ResultGenerator;
import com.binguo.wxapi.service.WXCustomerService;
import com.binguo.wxapi.vo.ShopInfoVO;
import com.qiniu.util.Auth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;


/**
 */
@RestController
@RequestMapping
public class WXCustomerController {

    private static Logger logger = LoggerFactory.getLogger(WXCustomerController.class);

    @Autowired
    WXCustomerService wxCustomerService;


    @GetMapping(value = "/api/wxapp/getcustomergroup", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取用户分组")
    public Result getCustomerGroup(Integer shopId) {

        return ResultGenerator.genSuccessResult(wxCustomerService.findCustomerGroupByShopId(shopId));
    }


    @PostMapping(value = "/api/wxapp/savecustomergroup")
    @ApiOperation(value = "保存分组")
    public Result getCustomerGroup(CustomerGroupForm customerGroupForm) {

        return wxCustomerService.saveCustomerGroup(customerGroupForm);
    }



    @PostMapping(value = "/api/wxapp/getVerification", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取手机验证码", notes = "获取手机号验证码")
    public Result getVerification(@RequestParam("phone") String phone){

        if (!PhoneHelper.verifyPhone(phone)) {
            return ResultGenerator.genFailResult("手机号不正确");
        }

        return wxCustomerService.sendSms(phone);
    }


    @PostMapping(value = "/api/wxapp/getShopId", produces = APPLICATION_JSON_VALUE)
    public Result getShopId(@RequestBody CustomerForm customerForm){

        Integer shopId =
                wxCustomerService.getShopByCustomerId(customerForm.getCustomerId());
        if (shopId != null) {
            return ResultGenerator.genSuccessResult(shopId);
        }

        return ResultGenerator.genFailResult("");
    }

    @PostMapping(value = "/api/wxapp/getShopInfo", produces = APPLICATION_JSON_VALUE)
    public Result getShopInfo(@RequestBody CustomerForm customerForm){

        Integer shopId =
                wxCustomerService.getShopByCustomerId(customerForm.getCustomerId());
        if (shopId != null) {
            ShopInfoVO shopInfoVO = wxCustomerService.getShopConfig(shopId);

            return ResultGenerator.genSuccessResult(shopInfoVO);
        }

        return ResultGenerator.genFailResult("");
    }




    @PostMapping(value = "/api/wxapp/merchantLogin", produces = APPLICATION_JSON_VALUE)
    public Result getShopId(@RequestBody MerchantLoginForm merchantLoginForm){


        return wxCustomerService.merchantLogin(merchantLoginForm.getPhone(),
                merchantLoginForm.getVerify(), merchantLoginForm.getCustomerId());
    }



    @PostMapping(value = "/api/wxapp/merchantLogout", produces = APPLICATION_JSON_VALUE)
    public Result merchantLogout(@RequestBody CustomerForm form){


        wxCustomerService.merchantLogout(form.getCustomerId());


        return ResultGenerator.genSuccessResult();
    }






}
